<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * @author Ivan Kukic
     * @version 1.0
     * @created 09-Oct-2012 19:25:26
     */
    class strparse {

        public function __construct() {
            
        }

        /**
         * Create one line of language coded string
         * @param str
         * @param lang
         */
        function create_lang_string($str, $lang) {
            return '<!--:' . $lang . '-->' . trim($str) . '<!--:-->';
        }

        /**
         * Find string in language coded string for given language
         * @param str
         * @param lang
         */
        function find_lang_in_string($string, $id, $def = FALSE) {
           if ((int)substr($string, 5, 2) > 0)   // Backward for price list table

                return $string;

            $origin_string = $string;

            if (substr($string, 0, 4) == '<!--') :

                $start = '<!--:' . $id . '-->';
                $end = '<!--:-->';

                $string = " " . $string;
                $ini = strpos($string, $start);

                if ($ini == 0)

                    if ($def === FALSE)

                        return '';

                    else

                        return $this->language_string($origin_string, $def, FALSE);   // recursive for def language 'en'

                $ini += strlen($start);
                $len = strpos($string, $end, $ini) - $ini;
                $fin = substr($string, $ini, $len);

                if ($fin === '')

                    if ($def === FALSE)

                        return '';

                    else

                        return $this->language_string($origin_string, $def, FALSE);   // recursive for def language 'en'

                else

                    return $fin;

            else :

                return $string;

            endif;
            
        }

    }